package com.boho.ecp.sms.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import com.boho.ecp.sms.model.SmsSender;

public class SmsService {

	private static final String SMS_CHARSET = "GBK";
	
	private static HttpClient client = new HttpClient();
	
	/**
	 * HTTP请求 POST方式
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static String post(PostMethod request) throws Exception {
		StringBuffer sb = new StringBuffer();
		try {
			int statusCode = client.executeMethod(request);
			if (statusCode != HttpStatus.SC_OK) {
				return String.valueOf(statusCode);
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(request.getResponseBodyAsStream(), SMS_CHARSET));
			try {
				String line;
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
			} finally {
				reader.close();
				reader = null;
			}
		} finally {
			request.releaseConnection();
			request = null;
		}
		return sb.toString();
		
	}
	/**
	 * 发送短信
	 * @param sender
	 * @return
	 * @throws Exception
	 */
	public static String sendSms(SmsSender sender) throws Exception {
		PostMethod request = new PostMethod(sender.getApiEndPoint());
		request.addParameter(new NameValuePair("username", sender.getUsername()));
		request.addParameter(new NameValuePair("password", sender.getPassword()));
		request.addParameter(new NameValuePair("message", URLEncoder.encode(sender.getMessage(), SMS_CHARSET)));
		request.addParameter(new NameValuePair("mobile", sender.getMobile()));
		request.addParameter(new NameValuePair("taskid", sender.getTaskid().toString()));
		request.addParameter(new NameValuePair("time", sender.getTime()));
		
		return post(request);
	}
	
	/**
	 * 获取状态报告
	 * @param sender
	 * @return
	 * @throws Exception
	 */
	public static String getReport(SmsSender sender) throws Exception {
		PostMethod request = new PostMethod(sender.getApiEndPoint());
		request.addParameter(new NameValuePair("username", sender.getUsername()));
		request.addParameter(new NameValuePair("password", sender.getPassword()));
		request.addParameter(new NameValuePair("taskid", sender.getTaskid().toString()));
		return post(request);
	}
	
	/**
	 * 获取下行报告
	 * @param sender
	 * @return
	 * @throws Exception
	 */
	public static String getDelivery(SmsSender sender) throws Exception {
		PostMethod request = new PostMethod(sender.getApiEndPoint());
		request.addParameter(new NameValuePair("username", sender.getUsername()));
		request.addParameter(new NameValuePair("password", sender.getPassword()));
		request.addParameter(new NameValuePair("taskid", sender.getTaskid().toString()));
		return post(request);
	}
	
	public static void main(String[] args){
		SmsSender sender = new SmsSender();
//		sender.setApiEndPoint("http://218.207.176.119:8090/Api/SendSms");
		sender.setApiEndPoint("http://218.207.176.119:8090/Api/GetReport");
//		sender.setApiEndPoint("http://218.207.176.119:8090/Api/GetDelivery");
		sender.setUsername("QFycbymt");
		sender.setPassword("QFycbymt");
		sender.setTaskid(4);
		sender.setMobile("13600805199");
		sender.setMessage("测试短信，收到了吗");
		sender.setTime("");
		
		try {
			String s = "";
//			s = sendSms(sender);
			s = getReport(sender);
//			s = getDelivery(sender);
			
			System.out.println(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
